import java.awt.Point;
import java.util.*;

public class rmi {

	final static SnakeClient sc = new SnakeClient();

	public static void cls()
	{
		for (int oo = 0; oo < 150; ++oo)
		{
			System.out.println();
		}
	}
	
    public static void main(String[] args) {
		
		int[][] snake = sc.GetSnake();
		List<int[]> snakeAsList = new LinkedList(Arrays.asList(snake));
		int[] sceneDim = sc.GetSceneDim();
		
		List<int[]> foodAsList = new LinkedList();
		while(true)
		{
			cls();
			sc.DrawStage(snakeAsList, foodAsList, sceneDim[0], sceneDim[1]);
			
			int[] nf = sc.GetFood(foodAsList, snakeAsList);
			// if (nf != null)
			// {
				// System.out.print("added new point: ");sc.PrintInt2(nf);
				// System.out.print("snake : ");sc.PrintList(snakeAsList);
				// System.out.print("food : ");sc.PrintList(foodAsList);
			// }
			
			
			int[] dir = null;
			try
			{
				dir = sc.RunAlgorithm(foodAsList, snakeAsList, sceneDim);
			}
			catch (Exception e)
			{
				System.out.println(e.toString());
				break;
			}
			
			sc.SimulateByOne(dir, snakeAsList);
			
			int[] gatheredFood = sc.IsFoodOnHead(snakeAsList.get(0), foodAsList);
			if (gatheredFood != null)
			{
				foodAsList.remove(gatheredFood);
				//System.out.print("gathered food: "); sc.PrintInt2(gatheredFood);
				//System.out.print("food after removal: "); sc.PrintList(foodAsList);
				//System.out.println("	list after removal: ");sc.PrintList(foodAsList);
				try
				{
					sc.LenghtenSnake(snakeAsList, sceneDim);
				}
				catch (Exception e)
				{
					System.out.println(e.toString());
					break;
				}
			}
			
			try
			{
				Thread.sleep(1000);
			}
			catch (Exception e)
			{
				System.out.println(e.toString());
				break;
			}
		}
		
    }

}
